
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>mysql事务与锁 · tgor note</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="tgor">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-page-toc-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="mysql数据类型.html" />
    
    
    <link rel="prev" href="经典数据库设计案例.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Me
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../category/微服务.html">
            
                <a href="../category/微服务.html">
            
                    
                    微服务
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../Java-Spring/Feign.html">
            
                <a href="../Java-Spring/Feign.html">
            
                    
                    HTTP客户端Feign
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../Java-其他库/限流.html">
            
                <a href="../Java-其他库/限流.html">
            
                    
                    限流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../运维/Nginx限流.html">
            
                <a href="../运维/Nginx限流.html">
            
                    
                    限流Nginx
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../Java-Spring/负载均衡.html">
            
                <a href="../Java-Spring/负载均衡.html">
            
                    
                    负载均衡
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../运维/Nginx负载均衡.html">
            
                <a href="../运维/Nginx负载均衡.html">
            
                    
                    负载均衡Nginx
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../Java-其他库/熔断Hystrix.html">
            
                <a href="../Java-其他库/熔断Hystrix.html">
            
                    
                    熔断Hystrix
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../Java-其他库/缓存.html">
            
                <a href="../Java-其他库/缓存.html">
            
                    
                    缓存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="../Java-其他库/缓存.html">
            
                <a href="../Java-其他库/缓存.html#本地缓存">
            
                    
                    本地缓存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9" data-path="../Redis/Redis.html">
            
                <a href="../Redis/Redis.html">
            
                    
                    分布式缓存Redis
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10" data-path="../Java-其他库/分布式事务.html">
            
                <a href="../Java-其他库/分布式事务.html">
            
                    
                    分布式事务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.11" data-path="../Java-其他库/分布式事务.html">
            
                <a href="../Java-其他库/分布式事务.html#事务消息">
            
                    
                    可靠事务消息
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.12" data-path="../Java-Spring/业务监控.html">
            
                <a href="../Java-Spring/业务监控.html">
            
                    
                    业务监控
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.13" data-path="../Java-其他库/分布式定时作业.html">
            
                <a href="../Java-其他库/分布式定时作业.html">
            
                    
                    分布式作业调度ElasticJob
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.14" data-path="../Java-ORM/ORM.html">
            
                <a href="../Java-ORM/ORM.html">
            
                    
                    ORM
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.15" data-path="../Java-其他库/分布式系统及CAP原理.html">
            
                <a href="../Java-其他库/分布式系统及CAP原理.html">
            
                    
                    分布式系统及CAP原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.16" data-path="../MQ/RabbitMQ笔记.html">
            
                <a href="../MQ/RabbitMQ笔记.html">
            
                    
                    消息驱动Rabbitmq
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../category/数据库.html">
            
                <a href="../category/数据库.html">
            
                    
                    数据库
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="经典数据库设计案例.html">
            
                <a href="经典数据库设计案例.html">
            
                    
                    mysql设计
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.2" data-path="Mysql事务与锁.html">
            
                <a href="Mysql事务与锁.html">
            
                    
                    mysql事务与锁
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="mysql数据类型.html">
            
                <a href="mysql数据类型.html">
            
                    
                    mysql数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="mysql索引及查询.html">
            
                <a href="mysql索引及查询.html">
            
                    
                    mysql索引与查询
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="mysql索引及查询.html">
            
                <a href="mysql索引及查询.html#查询语句设计准则">
            
                    
                    mysql查询语句设计准则
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="mysql性能测试.html">
            
                <a href="mysql性能测试.html">
            
                    
                    mysql性能测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="mysql主从同步与读写分离.html">
            
                <a href="mysql主从同步与读写分离.html">
            
                    
                    mysql主从同步与读写分离
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="../Java-Spring/SpringBoot事务注解@Transactional.html">
            
                <a href="../Java-Spring/SpringBoot事务注解@Transactional.html">
            
                    
                    Spring事务注解@Transactional
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="../数据结构与算法/位运算应用实例.html">
            
                <a href="../数据结构与算法/位运算应用实例.html">
            
                    
                    位运算应用实例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" data-path="../Mongodb-Client/morphia-API汇总.html">
            
                <a href="../Mongodb-Client/morphia-API汇总.html">
            
                    
                    morphia API汇总
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="../Mongodb-Client/mongo驱动连接.html">
            
                <a href="../Mongodb-Client/mongo驱动连接.html">
            
                    
                    mongodb驱动与连接
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="../Mongodb/mongodb-ObjectId实践.html">
            
                <a href="../Mongodb/mongodb-ObjectId实践.html">
            
                    
                    mongodb ObjectId实践
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="../Mongodb/mongodb角色权限.html">
            
                <a href="../Mongodb/mongodb角色权限.html">
            
                    
                    mongodb角色权限
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../category/java并发.html">
            
                <a href="../category/java并发.html">
            
                    
                    Java并发
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../Java-Base/Java并发.html">
            
                <a href="../Java-Base/Java并发.html">
            
                    
                    Java并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../Java-Base/Java并发.html">
            
                <a href="../Java-Base/Java并发.html#锁">
            
                    
                    Java并发之锁设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../Java-Base/Java并发.html">
            
                <a href="../Java-Base/Java并发.html#synchronized">
            
                    
                    Java并发之synchronized
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../Java-Base/Java之Lock.html">
            
                <a href="../Java-Base/Java之Lock.html#reentrantlock">
            
                    
                    Java Lock之ReentrantLock
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../Java-Base/Java之Lock.html">
            
                <a href="../Java-Base/Java之Lock.html#reentrantreadwritelock">
            
                    
                    Java Lock之ReentrantReadWriteLock
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../Java-Base/Java之Future.html">
            
                <a href="../Java-Base/Java之Future.html">
            
                    
                    Java Future
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../Java-Base/Java之Future.html">
            
                <a href="../Java-Base/Java之Future.html#futuretask">
            
                    
                    Java Future之FutureTask
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="../Java-Base/Java之Future.html">
            
                <a href="../Java-Base/Java之Future.html#completablefuture">
            
                    
                    Java Future之CompletableFuture
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="../Java-Base/Java之Future.html">
            
                <a href="../Java-Base/Java之Future.html#forkjointask">
            
                    
                    Java Future之ForkJoinTask
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.10" data-path="../Java-Base/Java之Thread.html">
            
                <a href="../Java-Base/Java之Thread.html">
            
                    
                    Java Thread
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../category/redis.html">
            
                <a href="../category/redis.html">
            
                    
                    Redis
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../Redis/Redis.html">
            
                <a href="../Redis/Redis.html">
            
                    
                    Redis
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../Redis/Redis.html">
            
                <a href="../Redis/Redis.html#集群">
            
                    
                    Redis集群
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../Redis/Redis.html">
            
                <a href="../Redis/Redis.html#消息订阅">
            
                    
                    Redis消息订阅
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../Redis/Redis.html">
            
                <a href="../Redis/Redis.html#lua">
            
                    
                    Redis Lua脚本
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../Redis/Redis.html">
            
                <a href="../Redis/Redis.html#配置汇总">
            
                    
                    Redis配置汇总
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../category/运维.html">
            
                <a href="../category/运维.html">
            
                    
                    运维
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../运维/Linux常用操作汇总.html">
            
                <a href="../运维/Linux常用操作汇总.html">
            
                    
                    Linux常用操作汇总
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../运维/阿里云笔记.html">
            
                <a href="../运维/阿里云笔记.html">
            
                    
                    阿里云笔记
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../category/nginx.html">
            
                <a href="../category/nginx.html">
            
                    
                    Nginx
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../运维/Nginx学习笔记.html">
            
                <a href="../运维/Nginx学习笔记.html">
            
                    
                    Nginx基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../运维/Nginx负载均衡.html">
            
                <a href="../运维/Nginx负载均衡.html">
            
                    
                    Nginx负载均衡
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../运维/Nginx限流.html">
            
                <a href="../运维/Nginx限流.html">
            
                    
                    Nginx限流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../运维/Nginx学习笔记.html">
            
                <a href="../运维/Nginx学习笔记.html#六、其他">
            
                    
                    Nginx客户端辅助技巧
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../category/容器化.html">
            
                <a href="../category/容器化.html">
            
                    
                    容器化
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../Docker/docker笔记.html">
            
                <a href="../Docker/docker笔记.html">
            
                    
                    Docker学习笔记
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../Docker/docker笔记.html">
            
                <a href="../Docker/docker笔记.html#基础操作">
            
                    
                    Docker基础操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../Docker/docker笔记.html">
            
                <a href="../Docker/docker笔记.html#dockerfile">
            
                    
                    DockerFile
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../category/Web.html">
            
                <a href="../category/Web.html">
            
                    
                    Web
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../运维/Http入坑Https笔记.html">
            
                <a href="../运维/Http入坑Https笔记.html">
            
                    
                    Http入坑Https笔记
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../Web/阿里云OSS.html">
            
                <a href="../Web/阿里云OSS.html">
            
                    
                    OSS
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../category/java基础.html">
            
                <a href="../category/java基础.html">
            
                    
                    Java基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="../Java-Base/Java基础.html">
            
                <a href="../Java-Base/Java基础.html#jvm">
            
                    
                    JVM基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="../Java-Base/Java基础.html">
            
                <a href="../Java-Base/Java基础.html#语法糖">
            
                    
                    Java语法糖
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="../Java-Base/Java基础.html">
            
                <a href="../Java-Base/Java基础.html#数据结构">
            
                    
                    Java数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.4" data-path="../Java-Base/Java8函数式编程.html">
            
                <a href="../Java-Base/Java8函数式编程.html">
            
                    
                    java8函数式编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.5" data-path="../Java-Base/ArrayList源码分析.html">
            
                <a href="../Java-Base/ArrayList源码分析.html">
            
                    
                    JDK源码分析ArrayList
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.6" data-path="../Java-Base/HashMap源码分析.html">
            
                <a href="../Java-Base/HashMap源码分析.html">
            
                    
                    JDK源码分析HashMap
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.7" data-path="../Java-Base/Java注解.html">
            
                <a href="../Java-Base/Java注解.html">
            
                    
                    Java注解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.8" data-path="../Java-Base/Timsort排序算法.html">
            
                <a href="../Java-Base/Timsort排序算法.html">
            
                    
                    Timsort排序算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.9" data-path="../Java-Base/插入排序分析与Java实现.html">
            
                <a href="../Java-Base/插入排序分析与Java实现.html">
            
                    
                    插入排序分析与Java实现
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../category/工具与规范.html">
            
                <a href="../category/工具与规范.html">
            
                    
                    工具与规范
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.11.1" data-path="../工具/Maven使用技巧汇总.html">
            
                <a href="../工具/Maven使用技巧汇总.html">
            
                    
                    Maven使用技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.2" data-path="../编码规范/Java编码规范.html">
            
                <a href="../编码规范/Java编码规范.html">
            
                    
                    Java编码规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../category/OLAP.html">
            
                <a href="../category/OLAP.html">
            
                    
                    OLAP
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.12.1" data-path="../数据处理/storm学习笔记.html">
            
                <a href="../数据处理/storm学习笔记.html">
            
                    
                    流式计算Storm学习笔记
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../category/数据运营.html">
            
                <a href="../category/数据运营.html">
            
                    
                    数据运营
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.13.1" data-path="../数据运营/增长黑客数据运营.html">
            
                <a href="../数据运营/增长黑客数据运营.html">
            
                    
                    增长黑客-数据运营
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../category/前端技术栈.html">
            
                <a href="../category/前端技术栈.html">
            
                    
                    前端技术栈
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.14.1" data-path="../前端/JavaScript设计模式.html">
            
                <a href="../前端/JavaScript设计模式.html">
            
                    
                    JavaScript设计模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.2" data-path="../前端/html相对路径与绝对路径.html">
            
                <a href="../前端/html相对路径与绝对路径.html">
            
                    
                    Html相对路径与绝对路径
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.3" data-path="../前端/微信小程序学习笔记.html">
            
                <a href="../前端/微信小程序学习笔记.html">
            
                    
                    微信小程序学习笔记
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../category/个人.html">
            
                <a href="../category/个人.html">
            
                    
                    个人
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.15.1" data-path="../个人/简历.html">
            
                <a href="../个人/简历.html">
            
                    
                    简历
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >mysql事务与锁</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="mysql&#x4E8B;&#x52A1;&#x4E0E;&#x9501;">Mysql&#x4E8B;&#x52A1;&#x4E0E;&#x9501;</h1>
<p><a href="http://naotu.baidu.com/file/3c955e4ef3959fb1def120c45ddde3ab?token=6ec4a0a688a5cf36" target="_blank">mysql&#x4E8B;&#x52A1;&#x4E0E;&#x9501;&#x8111;&#x56FE;</a></p>
<h1 id="&#x4E8B;&#x52A1;">&#x4E8B;&#x52A1;</h1>
<h2 id="acid&#x7279;&#x6027;">ACID&#x7279;&#x6027;</h2>
<ol>
<li><strong>&#x539F;&#x5B50;&#x6027;</strong>&#xFF08;Atomicity&#xFF09;&#xFF1A;&#x4E00;&#x4E2A;&#x4E8B;&#x52A1;&#xFF08;transaction&#xFF09;&#x4E2D;&#x7684;&#x6240;&#x6709;&#x64CD;&#x4F5C;&#xFF0C;&#x8981;&#x4E48;&#x5168;&#x90E8;&#x5B8C;&#x6210;&#xFF0C;&#x8981;&#x4E48;&#x5168;&#x90E8;&#x4E0D;&#x5B8C;&#x6210;&#xFF0C;&#x4E0D;&#x4F1A;&#x7ED3;&#x675F;&#x5728;&#x4E2D;&#x95F4;&#x67D0;&#x4E2A;&#x73AF;&#x8282;&#x3002;&#x4E8B;&#x52A1;&#x5728;&#x6267;&#x884C;&#x8FC7;&#x7A0B;&#x4E2D;&#x53D1;&#x751F;&#x9519;&#x8BEF;&#xFF0C;&#x4F1A;&#x88AB;&#x56DE;&#x6EDA;&#xFF08;Rollback&#xFF09;&#x5230;&#x4E8B;&#x52A1;&#x5F00;&#x59CB;&#x524D;&#x7684;&#x72B6;&#x6001;&#xFF0C;&#x5C31;&#x50CF;&#x8FD9;&#x4E2A;&#x4E8B;&#x52A1;&#x4ECE;&#x6765;&#x6CA1;&#x6709;&#x6267;&#x884C;&#x8FC7;&#x4E00;&#x6837;&#x3002;</li>
<li><strong>&#x4E00;&#x81F4;&#x6027;</strong>&#xFF08;Consistency&#xFF09;&#xFF1A;&#x5728;&#x4E8B;&#x52A1;&#x5F00;&#x59CB;&#x4E4B;&#x524D;&#x548C;&#x4E8B;&#x52A1;&#x7ED3;&#x675F;&#x4EE5;&#x540E;&#xFF0C;&#x6570;&#x636E;&#x5E93;&#x7684;&#x5B8C;&#x6574;&#x6027;&#x6CA1;&#x6709;&#x88AB;&#x7834;&#x574F;&#x3002;&#x8FD9;&#x8868;&#x793A;&#x5199;&#x5165;&#x7684;&#x8D44;&#x6599;&#x5FC5;&#x987B;&#x5B8C;&#x5168;&#x7B26;&#x5408;&#x6240;&#x6709;&#x7684;&#x9884;&#x8BBE;&#x89C4;&#x5219;&#xFF0C;&#x8FD9;&#x5305;&#x542B;&#x8D44;&#x6599;&#x7684;&#x7CBE;&#x786E;&#x5EA6;&#x3001;&#x4E32;&#x8054;&#x6027;&#x4EE5;&#x53CA;&#x540E;&#x7EED;&#x6570;&#x636E;&#x5E93;&#x53EF;&#x4EE5;&#x81EA;&#x53D1;&#x6027;&#x5730;&#x5B8C;&#x6210;&#x9884;&#x5B9A;&#x7684;&#x5DE5;&#x4F5C;&#x3002;</li>
<li><strong>&#x9694;&#x79BB;&#x6027;</strong>&#xFF08;Isolation&#xFF09;&#xFF1A;&#x6570;&#x636E;&#x5E93;&#x5141;&#x8BB8;&#x591A;&#x4E2A;&#x5E76;&#x53D1;&#x4E8B;&#x52A1;&#x540C;&#x65F6;&#x5BF9;&#x5176;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x8BFB;&#x5199;&#x548C;&#x4FEE;&#x6539;&#x7684;&#x80FD;&#x529B;&#xFF0C;&#x9694;&#x79BB;&#x6027;&#x53EF;&#x4EE5;&#x9632;&#x6B62;&#x591A;&#x4E2A;&#x4E8B;&#x52A1;&#x5E76;&#x53D1;&#x6267;&#x884C;&#x65F6;&#x7531;&#x4E8E;&#x4EA4;&#x53C9;&#x6267;&#x884C;&#x800C;&#x5BFC;&#x81F4;&#x6570;&#x636E;&#x7684;&#x4E0D;&#x4E00;&#x81F4;&#x3002;&#x4E8B;&#x52A1;&#x9694;&#x79BB;&#x5206;&#x4E3A;&#x4E0D;&#x540C;&#x7EA7;&#x522B;&#xFF0C;&#x5305;&#x62EC;&#x8BFB;&#x672A;&#x63D0;&#x4EA4;&#xFF08;Read uncommitted&#xFF09;&#x3001;&#x8BFB;&#x63D0;&#x4EA4;&#xFF08;read committed&#xFF09;&#x3001;&#x53EF;&#x91CD;&#x590D;&#x8BFB;&#xFF08;repeatable read&#xFF09;&#x548C;&#x4E32;&#x884C;&#x5316;&#xFF08;Serializable&#xFF09;&#x3002;</li>
<li><strong>&#x6301;&#x4E45;&#x6027;</strong>&#xFF08;Durability&#xFF09;&#xFF1A;&#x4E8B;&#x52A1;&#x5904;&#x7406;&#x7ED3;&#x675F;&#x540E;&#xFF0C;&#x5BF9;&#x6570;&#x636E;&#x7684;&#x4FEE;&#x6539;&#x5C31;&#x662F;&#x6C38;&#x4E45;&#x7684;&#xFF0C;&#x5373;&#x4FBF;&#x7CFB;&#x7EDF;&#x6545;&#x969C;&#x4E5F;&#x4E0D;&#x4F1A;&#x4E22;&#x5931;&#x3002;</li>
</ol>
<hr>
<h2 id="&#x542F;&#x7528;&#x4E8B;&#x52A1;">&#x542F;&#x7528;&#x4E8B;&#x52A1;</h2>
<p>console</p>
<p>1&#x3001;&#x7528; BEGIN, ROLLBACK, COMMIT&#x6765;&#x5B9E;&#x73B0;</p>
<ul>
<li>BEGIN &#x5F00;&#x59CB;&#x4E00;&#x4E2A;&#x4E8B;&#x52A1;</li>
<li>ROLLBACK &#x4E8B;&#x52A1;&#x56DE;&#x6EDA;</li>
<li>COMMIT &#x4E8B;&#x52A1;&#x786E;&#x8BA4;</li>
</ul>
<p>2&#x3001;&#x76F4;&#x63A5;&#x7528; SET &#x6765;&#x6539;&#x53D8; MySQL &#x7684;&#x81EA;&#x52A8;&#x63D0;&#x4EA4;&#x6A21;&#x5F0F;:</p>
<ul>
<li>SET AUTOCOMMIT=0 &#x7981;&#x6B62;&#x81EA;&#x52A8;&#x63D0;&#x4EA4;</li>
<li>SET AUTOCOMMIT=1 &#x5F00;&#x542F;&#x81EA;&#x52A8;&#x63D0;&#x4EA4;</li>
</ul>
<hr>
<h2 id="trans&#x663E;&#x5F0F;&#x6307;&#x5B9A;&#x4E8B;&#x52A1;&#x9694;&#x79BB;&#x7EA7;&#x522B;">@Trans&#x663E;&#x5F0F;&#x6307;&#x5B9A;&#x4E8B;&#x52A1;&#x9694;&#x79BB;&#x7EA7;&#x522B;</h2>
<p>&#x6CE8;&#x610F;&#x4E8B;&#x9879;&#xFF1A;</p>
<ol>
<li>&#x9700;&#x8981;&#x8C03;&#x7528;spring&#x5BB9;&#x5668;&#x4E2D;bean&#x5BF9;&#x8C61;&#x4E2D;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x4E8B;&#x52A1;&#x6CE8;&#x89E3;&#x624D;&#x80FD;&#x751F;&#x6548;</li>
<li>&#x9700;&#x8981;&#x663E;&#x5F0F;&#x6307;&#x5B9A;<code>rollbackFor</code>&#x9700;&#x8981;&#x6355;&#x83B7;<code>Exception</code>&#xFF0C;&#x5426;&#x5219;&#x5982;&#x679C;&#x629B;&#x51FA;<code>Exception</code>&#x53EF;&#x80FD;&#x4E0D;&#x4F1A;&#x89E6;&#x53D1;&#x56DE;&#x6EDA;</li>
<li>&#x5F02;&#x5E38;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x5EFA;&#x8BAE;&#x663E;&#x5F0F;&#x56DE;&#x6EDA;</li>
</ol>
<pre><code class="lang-java"><span class="hljs-meta">@Transactional</span>(propagation = Propagation.REQUIRES_NEW,
         rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">fn</span><span class="hljs-params">()</span></span>{
    var ok = mapper.update();
    <span class="hljs-keyword">if</span>(!ok){
        <span class="hljs-comment">//&#x663E;&#x5F0F;&#x56DE;&#x6EDA;</span>
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
    }
}
</code></pre>
<p>&#x663E;&#x5F0F;&#x56DE;&#x6EDA;&#x8BED;&#x53E5;&#xFF1A;<code>TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();</code></p>
<h2 id="&#x67E5;&#x8BE2;&#x4FEE;&#x6539;&#x5168;&#x5C40;&#x548C;&#x4F1A;&#x8BDD;&#x4E8B;&#x52A1;&#x7EA7;&#x522B;">&#x67E5;&#x8BE2;&#x4FEE;&#x6539;&#x5168;&#x5C40;&#x548C;&#x4F1A;&#x8BDD;&#x4E8B;&#x52A1;&#x7EA7;&#x522B;</h2>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x7528;&#x4E0B;&#x5217;&#x8BED;&#x53E5;&#x67E5;&#x8BE2;&#x5168;&#x5C40;&#x548C;&#x4F1A;&#x8BDD;&#x4E8B;&#x52A1;&#x9694;&#x79BB;&#x7EA7;&#x522B;&#xFF1A;</p>
<pre><code>SELECT @@global.tx_isolation; 
SELECT @@session.tx_isolation; 
SELECT @@tx_isolation;
</code></pre><p>&#x8BBE;&#x7F6E;&#x4E8B;&#x52A1;&#x9694;&#x79BB;&#x7EA7;&#x522B;&#xFF1A;</p>
<pre><code>set session transaction isolation level read uncommitted;
set session transaction isolation level read committed;
set session transaction isolation level repeatable read;
set session transaction isolation level serializable;

--&#x8BBE;&#x7F6E;&#x5168;&#x5C40;
set GLOBAL transaction isolation level read committed;
</code></pre><hr>
<h1 id="&#x4E8B;&#x52A1;&#x9694;&#x79BB;&#x7EA7;&#x522B;">&#x4E8B;&#x52A1;&#x9694;&#x79BB;&#x7EA7;&#x522B;</h1>
<p>&#x4E8B;&#x52A1;&#x9694;&#x79BB;&#x5206;&#x4E3A;&#x4E0D;&#x540C;&#x7EA7;&#x522B;&#xFF0C;&#x5305;&#x62EC;&#x8BFB;&#x672A;&#x63D0;&#x4EA4;&#xFF08;Read uncommitted&#xFF09;&#x3001;&#x8BFB;&#x63D0;&#x4EA4;&#xFF08;read committed&#xFF09;&#x3001;&#x53EF;&#x91CD;&#x590D;&#x8BFB;&#xFF08;repeatable read&#xFF09;(mysql&#x9ED8;&#x8BA4;&#x7EA7;&#x522B;)&#x548C;&#x4E32;&#x884C;&#x5316;&#xFF08;Serializable&#xFF09;&#x3002;</p>
<ol>
<li>read uncommitted : &#x8BFB;&#x53D6;&#x5C1A;&#x672A;&#x63D0;&#x4EA4;&#x7684;&#x6570;&#x636E; &#xFF1A;&#x54EA;&#x4E2A;&#x95EE;&#x9898;&#x90FD;&#x4E0D;&#x80FD;&#x89E3;&#x51B3;</li>
<li>read committed&#xFF1A;&#x8BFB;&#x53D6;&#x5DF2;&#x7ECF;&#x63D0;&#x4EA4;&#x7684;&#x6570;&#x636E; &#xFF1A;&#x53EF;&#x4EE5;&#x89E3;&#x51B3;&#x810F;&#x8BFB; ---- oracle&#x3001;sqlserver&#x9ED8;&#x8BA4;&#x7684;</li>
<li>repeatable read&#xFF1A;&#x91CD;&#x8BFB;&#x8BFB;&#x53D6;&#xFF1A;&#x53EF;&#x4EE5;&#x89E3;&#x51B3;&#x810F;&#x8BFB; &#x548C; &#x4E0D;&#x53EF;&#x91CD;&#x590D;&#x8BFB; ---mysql&#x9ED8;&#x8BA4;&#x7684;</li>
<li>serializable&#xFF1A;&#x4E32;&#x884C;&#x5316;&#xFF1A;&#x53EF;&#x4EE5;&#x89E3;&#x51B3; &#x810F;&#x8BFB; &#x4E0D;&#x53EF;&#x91CD;&#x590D;&#x8BFB; &#x548C; &#x865A;&#x8BFB;---<strong>&#x76F8;&#x5F53;&#x4E8E;&#x9501;&#x8868;</strong></li>
</ol>
<hr>
<h2 id="read-committed&#x4E0E;repeatable-read">Read committed&#x4E0E;Repeatable read</h2>
<h3 id="&#x4E24;&#x8005;&#x539F;&#x7406;&#x533A;&#x522B;">&#x4E24;&#x8005;&#x539F;&#x7406;&#x533A;&#x522B;</h3>
<p>&#x4E00;&#x53E5;&#x8BDD;&#x603B;&#x7ED3;</p>
<ol>
<li>RC&#x8BFB;&#x53D6;&#xFF0C;&#x4E0D;&#x7BA1;&#x4E8B;&#x52A1;&#x542F;&#x52A8;&#x524D;&#x540E;&#xFF0C;&#x8BFB;&#x6700;&#x65B0;&#x63D0;&#x4EA4;&#x5FEB;&#x7167;</li>
<li>RR&#x8BFB;&#x53D6;&#xFF0C;&#x4E8B;&#x52A1;&#x542F;&#x52A8;&#x524D;&#x7684;&#x6700;&#x65B0;&#x5FEB;&#x7167;&#x7248;&#x672C;</li>
</ol>
<p><img src="assets/1574913599999.png" alt="1574913599999"></p>
<p>&#x53C2;&#x8003;&#xFF1A;<a href="#&#x4E00;&#x81F4;&#x6027;&#x975E;&#x9501;&#x5B9A;&#x8BFB;">&#x4E00;&#x81F4;&#x6027;&#x975E;&#x9501;&#x5B9A;&#x8BFB;</a></p>
<h3 id="&#x4F7F;&#x7528;&#x9501;&#x7684;&#x533A;&#x522B;">&#x4F7F;&#x7528;&#x9501;&#x7684;&#x533A;&#x522B;</h3>
<p>&#x5982;&#x65E0;&#x7279;&#x6B8A;&#x60C5;&#x51B5;&#xFF0C;&#x4E00;&#x5F8B;&#x8981;&#x6C42;&#x91C7;&#x7528;RC&#x9694;&#x79BB;&#x7EA7;&#x522B;</p>
<p>&#x539F;&#x56E0;&#x662F;RR&#x7EA7;&#x522B;&#xFF0C;&#x5199;&#x5165;&#x64CD;&#x4F5C;&#x4F1A;&#x91C7;&#x7528;<code>Gap Lock</code>&#x9501;&#x7B97;&#x6CD5;&#xFF0C;&#x9501;&#x5B9A;&#x63D2;&#x5165;&#x884C;&#x7684;&#x4E00;&#x5B9A;&#x8303;&#x56F4;&#xFF0C;&#x5F71;&#x54CD;&#x5199;&#x5165;&#x6027;&#x80FD;</p>
<hr>
<h1 id="&#x884C;&#x9501;">&#x884C;&#x9501;</h1>
<p>&#x67E5;&#x770B;&#x9501;&#x60C5;&#x51B5;</p>
<p>&#x547D;&#x4EE4;<code>show engine innodb status</code>&#x67E5;&#x770B;&#x5F53;&#x524D;&#x9501;&#x60C5;&#x51B5;</p>
<p>Innodb 1.0&#x7248;&#x672C;&#x4EE5;&#x540E;&#xFF0C;&#x5EFA;&#x8BAE;&#x901A;&#x8FC7;information_schema&#x67B6;&#x6784;&#x4E0B;&#x7684;&#x8868;&#x67E5;&#x8BE2;&#x4E8B;&#x52A1;&#x4E0E;&#x9501;&#x7684;&#x72B6;&#x6001;&#xFF1A;<a href="https://www.zybuluo.com/zero1036/note/1437297" target="_blank">information_schema innodb&#x8BE6;&#x89E3;</a></p>
<ol>
<li>INNODB_TRX</li>
<li>INNODB_LOCKS</li>
<li>INNODB_LOCK_WAITS</li>
</ol>
<h2 id="&#x9501;&#x7684;&#x7C7B;&#x578B;">&#x9501;&#x7684;&#x7C7B;&#x578B;</h2>
<table>
<thead>
<tr>
<th>&#x9501;&#x7C7B;&#x578B;</th>
<th style="text-align:left">&#x63CF;&#x8FF0;</th>
<th style="text-align:center">&#x7C92;&#x5EA6;</th>
</tr>
</thead>
<tbody>
<tr>
<td>&#x5171;&#x4EAB;&#x9501;&#xFF08;S Lock&#xFF09;</td>
<td style="text-align:left">&#x5141;&#x8BB8;&#x4E8B;&#x52A1;&#x8BFB;&#x4E00;&#x884C;&#x6570;&#x636E;</td>
<td style="text-align:center">&#x884C;&#x9501;</td>
</tr>
<tr>
<td>&#x6392;&#x4ED6;&#x9501;&#xFF08;X Lock&#xFF09;</td>
<td style="text-align:left">&#x5141;&#x8BB8;&#x4E8B;&#x52A1;&#x5220;&#x9664;&#x6216;&#x66F4;&#x65B0;&#x4E00;&#x884C;&#x6570;&#x636E;</td>
<td style="text-align:center">&#x884C;&#x9501;</td>
</tr>
<tr>
<td>&#x610F;&#x5411;&#x5171;&#x4EAB;&#x9501;&#xFF08;IS Lock&#xFF09;</td>
<td style="text-align:left">&#x4E8B;&#x52A1;&#x60F3;&#x8981;&#x83B7;&#x5F97;&#x4E00;&#x5F20;&#x8868;&#x4E2D;&#x67D0;&#x51E0;&#x884C;&#x7684;&#x5171;&#x4EAB;&#x9501;</td>
<td style="text-align:center">&#x8868;&#x9501;</td>
</tr>
<tr>
<td>&#x610F;&#x5411;&#x6392;&#x4ED6;&#x9501;&#xFF08;IX Lock&#xFF09;</td>
<td style="text-align:left">&#x4E8B;&#x52A1;&#x60F3;&#x8981;&#x83B7;&#x5F97;&#x4E00;&#x5F20;&#x8868;&#x4E2D;&#x67D0;&#x51E0;&#x884C;&#x7684;&#x6392;&#x4ED6;&#x9501;</td>
<td style="text-align:center">&#x8868;&#x9501;</td>
</tr>
</tbody>
</table>
<p>&#x517C;&#x5BB9;&#x6027;&#xFF1A;&#x5217;&#x4E3A;&#x5DF2;&#x6709;&#x6240;&#xFF0C;&#x884C;&#x4E3A;&#x671F;&#x671B;&#x83B7;&#x5F97;&#x9501;</p>
<table>
<thead>
<tr>
<th>&#x3002;</th>
<th style="text-align:center">IS</th>
<th style="text-align:center">IX</th>
<th style="text-align:left">S</th>
<th style="text-align:center">X</th>
</tr>
</thead>
<tbody>
<tr>
<td>IS</td>
<td style="text-align:center">&#x517C;&#x5BB9;</td>
<td style="text-align:center">&#x517C;&#x5BB9;</td>
<td style="text-align:left">&#x517C;&#x5BB9;</td>
<td style="text-align:center">&#x4E0D;&#x517C;&#x5BB9;</td>
</tr>
<tr>
<td>IX</td>
<td style="text-align:center">&#x4E0D;&#x517C;&#x5BB9;</td>
<td style="text-align:center">&#x517C;&#x5BB9;</td>
<td style="text-align:left">&#x4E0D;&#x517C;&#x5BB9;</td>
<td style="text-align:center">&#x4E0D;&#x517C;&#x5BB9;</td>
</tr>
<tr>
<td>S</td>
<td style="text-align:center">&#x517C;&#x5BB9;</td>
<td style="text-align:center">&#x4E0D;&#x517C;&#x5BB9;</td>
<td style="text-align:left">&#x517C;&#x5BB9;</td>
<td style="text-align:center">&#x4E0D;&#x517C;&#x5BB9;</td>
</tr>
<tr>
<td>X</td>
<td style="text-align:center">&#x4E0D;&#x517C;&#x5BB9;</td>
<td style="text-align:center">&#x4E0D;&#x517C;&#x5BB9;</td>
<td style="text-align:left">&#x4E0D;&#x517C;&#x5BB9;</td>
<td style="text-align:center">&#x4E0D;&#x517C;&#x5BB9;</td>
</tr>
</tbody>
</table>
<p>&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x4E8B;&#x52A1;T1&#x5DF2;&#x7ECF;&#x83B7;&#x53D6;&#x7684;&#x884C;r&#x7684;&#x5171;&#x4EAB;&#x9501;&#xFF0C;&#x90A3;&#x4E48;&#x53E6;&#x5916;&#x7684;&#x4E8B;&#x52A1;T2&#x53EF;&#x4EE5;&#x7ACB;&#x5373;&#x83B7;&#x5F97;&#x884C;r&#x7684;&#x5171;&#x4EAB;&#x9501;&#xFF0C;&#x56E0;&#x4E3A;&#x8BFB;&#x53D6;&#x5E76;&#x6CA1;&#x6709;&#x6539;&#x53D8;&#x884C;r&#x7684;&#x6570;&#x636E;&#xFF0C;&#x79F0;&#x4E3A;&#x9501;&#x517C;&#x5BB9;&#x3002;&#x4F46;&#x82E5;&#x5176;&#x4ED6;&#x4E8B;&#x52A1;T3&#x60F3;&#x83B7;&#x5F97;&#x884C;r&#x7684;&#x6392;&#x4ED6;&#x9501;&#xFF0C;&#x5219;&#x5FC5;&#x987B;&#x7B49;&#x5F85;T1&#x3001;T2&#x91CA;&#x653E;&#x884C;&#x4E0A;&#x7684;&#x5171;&#x4EAB;&#x9501;&#x3002;</p>
<h3 id="&#x610F;&#x5411;&#x9501;">&#x610F;&#x5411;&#x9501;</h3>
<p>&#x610F;&#x5411;&#x9501;&#xFF1A;&#x82E5;&#x5C06;&#x4E0A;&#x9501;&#x7684;&#x5BF9;&#x8C61;&#x770B;&#x6210;&#x4E00;&#x68F5;&#x6811;&#xFF0C;&#x90A3;&#x4E48;&#x5BF9;&#x6700;&#x4E0B;&#x5C42;&#x7684;&#x5BF9;&#x8C61;&#xFF08;&#x7C92;&#x5EA6;&#x6700;&#x5C0F;&#x7684;&#x884C;&#x9501;&#xFF09;&#x4E0A;&#x9501;&#xFF0C;&#x9996;&#x5148;&#x9700;&#x8981;&#x5BF9;&#x7C97;&#x7C92;&#x5EA6;&#x7684;&#x5BF9;&#x8C61;&#x4E0A;&#x9501;&#x3002;&#x5F62;&#x6210;&#x4E00;&#x4E2A;&#x4ECE;&#x4E0A;&#x81F3;&#x4E0B;&#x7684;&#x4E0A;&#x9501;&#x6D41;&#x7A0B;&#x3002;&#x8BBE;&#x8BA1;&#x76EE;&#x7684;&#x662F;&#x4E3A;&#x4E86;&#x5728;&#x4E00;&#x4E2A;&#x4E8B;&#x52A1;&#x4E2D;&#x63ED;&#x793A;&#x4E0B;&#x4E00;&#x884C;&#x5C06;&#x88AB;&#x8BF7;&#x6C42;&#x7684;&#x9501;&#x7C7B;&#x578B;&#x3002;</p>
<p><img src="http://static.zybuluo.com/zero1036/q6stas5ix7iymmslpamp9uzm/AD551EDF-59EF-4F33-8F4A-B3D303B08925.png" alt="&#x610F;&#x5411;&#x9501;"></p>
<p>&#x5982;&#x679C;&#x9700;&#x8981;&#x5BF9;&#x9875;&#x4E0A;&#x7684;&#x8BB0;&#x5F55;r&#x4E0A;X&#x9501;&#xFF0C;&#x90A3;&#x4E48;&#x5206;&#x522B;&#x9700;&#x8981;&#x5BF9;&#x6570;&#x636E;&#x5E93;A&#x3001;&#x8868;&#x3001;&#x9875;&#x4E0A;&#x610F;&#x5411;&#x9501;IX&#xFF0C;&#x6700;&#x540E;&#x5BF9;&#x8BB0;&#x5F55;r&#x4E0A;X&#x9501;&#x3002;&#x5982;&#x679C;&#x4EFB;&#x4F55;&#x4E00;&#x4E2A;&#x90E8;&#x5206;&#x5BFC;&#x81F4;&#x7B49;&#x5F85;&#xFF0C;&#x90A3;&#x4E48;&#x8BE5;&#x64CD;&#x4F5C;&#x9700;&#x8981;&#x7B49;&#x5F85;&#x7C97;&#x7C92;&#x5EA6;&#x9501;&#x7684;&#x5B8C;&#x6210;&#x3002;</p>
<table>
<thead>
<tr>
<th>A&#x4F1A;&#x8BDD;</th>
<th style="text-align:left">B&#x4F1A;&#x8BDD;</th>
</tr>
</thead>
<tbody>
<tr>
<td>&#x5BF9;&#x8868;T&#x7684;&#x884C;r&#x4E0A;S&#x9501;</td>
<td style="text-align:left"></td>
</tr>
<tr>
<td></td>
<td style="text-align:left">&#x8BA1;&#x5212;&#x5BF9;&#x884C;r&#x4E0A;X&#x9501;</td>
</tr>
<tr>
<td></td>
<td style="text-align:left">&#x5148;&#x5BF9;&#x8868;T&#x4E0A;IX&#x9501;</td>
</tr>
<tr>
<td></td>
<td style="text-align:left">IX&#x4E0E;S&#x4E0D;&#x517C;&#x5BB9;&#xFF0C;&#x963B;&#x585E;</td>
</tr>
</tbody>
</table>
<h2 id="select&#x8BED;&#x53E5;&#x4E0A;&#x9501;&#x65B9;&#x5F0F;">select&#x8BED;&#x53E5;&#x4E0A;&#x9501;&#x65B9;&#x5F0F;</h2>
<table>
<thead>
<tr>
<th>&#x8BED;&#x53E5;</th>
<th style="text-align:left">&#x9501;&#x7C7B;&#x578B;</th>
<th style="text-align:left">amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>select</td>
<td style="text-align:left">&#x6CA1;&#x6709;&#x4E0A;&#x9501;</td>
<td style="text-align:left"></td>
</tr>
<tr>
<td>select ... for update</td>
<td style="text-align:left">&#x6392;&#x4ED6;&#x9501;(X LOCK)</td>
<td style="text-align:left">Mysql&#x4F1A;&#x5BF9;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x4E2D;&#x7684;&#x6BCF;&#x884C;&#x90FD;&#x52A0;&#x6392;&#x4ED6;&#x9501;&#xFF0C;&#x5F53;&#x6CA1;&#x6709;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x5BF9;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x96C6;&#x4E2D;&#x7684;&#x4EFB;&#x4F55;&#x4E00;&#x884C;&#x4F7F;&#x7528;&#x6392;&#x4ED6;&#x9501;&#x65F6;&#xFF0C;&#x53EF;&#x4EE5;&#x6210;&#x529F;&#x7533;&#x8BF7;&#x6392;&#x4ED6;&#x9501;&#xFF0C;<strong>&#x5426;&#x5219;&#x4F1A;&#x88AB;&#x963B;&#x585E;</strong>&#x3002;&#x884C;&#x7EA7;&#x9501;&#x90FD;&#x662F;&#x57FA;&#x4E8E;&#x7D22;&#x5F15;&#x7684;&#xFF0C;&#x5982;&#x679C;&#x4E00;&#x6761;SQL&#x8BED;&#x53E5;&#x7528;&#x4E0D;&#x5230;&#x7D22;&#x5F15;&#x662F;&#x4E0D;&#x4F1A;&#x4F7F;&#x7528;&#x884C;&#x7EA7;&#x9501;&#x7684;&#xFF0C;&#x4F1A;&#x4F7F;&#x7528;&#x8868;&#x7EA7;&#x9501;&#x3002;&#x884C;&#x7EA7;&#x9501;&#x7684;&#x7F3A;&#x70B9;&#x662F;&#xFF1A;&#x7531;&#x4E8E;&#x9700;&#x8981;&#x8BF7;&#x6C42;&#x5927;&#x91CF;&#x7684;&#x9501;&#x8D44;&#x6E90;&#xFF0C;&#x6240;&#x4EE5;&#x901F;&#x5EA6;&#x6162;&#xFF0C;&#x5185;&#x5B58;&#x6D88;&#x8017;&#x5927;&#xFF01;</td>
</tr>
<tr>
<td>select ... lock in share mode;</td>
<td style="text-align:left">&#x5171;&#x4EAB;&#x9501;(S LOCK)</td>
<td style="text-align:left">Mysql&#x4F1A;&#x5BF9;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x4E2D;&#x7684;&#x6BCF;&#x884C;&#x90FD;&#x52A0;&#x5171;&#x4EAB;&#x9501;&#xFF0C;&#x5F53;&#x6CA1;&#x6709;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x5BF9;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x96C6;&#x4E2D;&#x7684;&#x4EFB;&#x4F55;&#x4E00;&#x884C;&#x4F7F;&#x7528;&#x6392;&#x4ED6;&#x9501;&#x65F6;&#xFF0C;&#x53EF;&#x4EE5;&#x6210;&#x529F;&#x7533;&#x8BF7;&#x5171;&#x4EAB;&#x9501;&#xFF0C;&#x5426;&#x5219;&#x4F1A;&#x88AB;&#x963B;&#x585E;&#x3002;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x4E5F;&#x53EF;&#x4EE5;&#x8BFB;&#x53D6;&#x4F7F;&#x7528;&#x4E86;&#x5171;&#x4EAB;&#x9501;&#x7684;&#x8868;&#xFF0C;&#x800C;&#x4E14;&#x8FD9;&#x4E9B;&#x7EBF;&#x7A0B;&#x8BFB;&#x53D6;&#x7684;&#x662F;&#x540C;&#x4E00;&#x4E2A;&#x7248;&#x672C;&#x7684;&#x6570;&#x636E;&#x3002;</td>
</tr>
</tbody>
</table>
<hr>
<h2 id="&#x884C;&#x9501;&#x7684;&#x7B97;&#x6CD5;">&#x884C;&#x9501;&#x7684;&#x7B97;&#x6CD5;</h2>
<ol>
<li><strong>Record Lock</strong>&#xFF1A;&#x5355;&#x4E2A;&#x8BB0;&#x5F55;&#x4E0A;&#x7684;&#x9501;</li>
<li><strong>Gap Lock</strong>&#xFF1A;&#x95F4;&#x9699;&#x9501;&#xFF0C;&#x9501;&#x5B9A;&#x4E00;&#x4E2A;&#x8303;&#x56F4;&#xFF0C;&#x4F46;&#x4E0D;&#x5305;&#x542B;&#x8BB0;&#x5F55;&#x672C;&#x8EAB;</li>
<li><strong>NextKey Lock</strong>&#xFF1A;Gap Lock+Record Lock&#xFF0C;&#x9501;&#x5B9A;&#x4E00;&#x4E2A;&#x8303;&#x56F4;&#xFF0C;&#x5E76;&#x4E14;&#x9501;&#x5B9A;&#x8BB0;&#x5F55;&#x672C;&#x8EAB;</li>
</ol>
<h3 id="nextkey-lock&#x964D;&#x7EA7;&#x4E3A;record-lock&#x7684;3&#x79CD;&#x573A;&#x666F;">NextKey Lock&#x964D;&#x7EA7;&#x4E3A;Record Lock&#x7684;3&#x79CD;&#x573A;&#x666F;</h3>
<p>1&#x3001;&#x67E5;&#x8BE2;&#x6761;&#x4EF6;&#x547D;&#x4E2D;&#x552F;&#x4E00;&#x7D22;&#x5F15;&#xFF0C;NextKey Lock&#x4F1A;&#x81EA;&#x52A8;&#x964D;&#x7EA7;&#x4E3A;Record Lock</p>
<table>
<thead>
<tr>
<th>A&#x4F1A;&#x8BDD;</th>
<th style="text-align:left">B&#x4F1A;&#x8BDD;</th>
</tr>
</thead>
<tbody>
<tr>
<td>select * from account where id = 5 for update</td>
<td style="text-align:left"></td>
</tr>
<tr>
<td></td>
<td style="text-align:left">insert into account (id,  name ,amount) values (4,&apos;tgtg&apos;,300); &#x4E0D;&#x4F1A;&#x963B;&#x585E;</td>
</tr>
</tbody>
</table>
<p>&#x5728;&#x4F1A;&#x8BDD;A&#x4E2D;&#xFF0C;&#x9996;&#x5148;&#x5BF9;id=5&#x8FDB;&#x884C;X&#x9501;&#x5B9A;&#xFF0C;&#x7531;&#x4E8E;id&#x662F;&#x4E3B;&#x952E;&#x4E14;&#x552F;&#x4E00;&#xFF0C;&#x56E0;&#x6B64;&#x9501;&#x5B9A;&#x7684;&#x4EC5;&#x662F;5&#x8FD9;&#x4E2A;&#x503C;&#xFF0C;&#x800C;&#x4E0D;&#x662F;(2, 5)&#x8FD9;&#x4E2A;&#x8303;&#x56F4;&#xFF1B;</p>
<h3 id="gap-lock&#x573A;&#x666F;">gap lock&#x573A;&#x666F;</h3>
<p>TODO</p>
<hr>
<h2 id="&#x4E00;&#x81F4;&#x6027;&#x3010;&#x975E;&#x3011;&#x9501;&#x5B9A;&#x8BFB;&#x4E0E;&#x4E00;&#x81F4;&#x6027;&#x9501;&#x5B9A;&#x5EA6;">&#x4E00;&#x81F4;&#x6027;&#x3010;&#x975E;&#x3011;&#x9501;&#x5B9A;&#x8BFB;&#x4E0E;&#x4E00;&#x81F4;&#x6027;&#x9501;&#x5B9A;&#x5EA6;</h2>
<p><img src="assets/1574915678135.png" alt="1574915678135"></p>
<h2 id="&#x4E00;&#x81F4;&#x6027;&#x975E;&#x9501;&#x5B9A;&#x8BFB;">&#x4E00;&#x81F4;&#x6027;&#x975E;&#x9501;&#x5B9A;&#x8BFB;</h2>
<p>&#x4E00;&#x81F4;&#x6027;&#x7684;&#x975E;&#x9501;&#x5B9A;&#x8BFB;&#x662F;&#x6307;InnoDB&#x5B58;&#x50A8;&#x5F15;&#x64CE;&#x901A;&#x8FC7;&#x884C;&#x591A;&#x7248;&#x672C;&#x63A7;&#x5236;&#xFF08;multi versioning&#xFF09;&#x7684;&#x65B9;&#x5F0F;&#x6765;&#x8BFB;&#x53D6;&#x5F53;&#x524D;&#x6267;&#x884C;&#x65F6;&#x95F4;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x7684;&#x884C;&#x6570;&#x636E;&#x3002;&#x5982;&#x679C;&#x8BFB;&#x53D6;&#x7684;&#x884C;&#x6B63;&#x5728;&#x6267;&#x884C;Delete&#x6216;Update&#x64CD;&#x4F5C;&#xFF0C;&#x8FD9;&#x662F;&#x8BFB;&#x53D6;&#x64CD;&#x4F5C;&#x4E0D;&#x4F1A;&#x56E0;&#x6B64;&#x53BB;&#x7B49;&#x5F85;&#x884C;&#x4E0A;&#x9501;&#x7684;&#x91CA;&#x653E;&#x3002;&#x76F8;&#x53CD;&#xFF0C;InnoDB&#x5B58;&#x50A8;&#x5F15;&#x64CE;&#x4F1A;&#x53BB;&#x8BFB;&#x53D6;&#x884C;&#x7684;&#x4E00;&#x4E2A;&#x5FEB;&#x7167;&#x6570;&#x636E;&#x3002;</p>
<p>&#x5FEB;&#x7167;&#x6570;&#x636E;&#x672C;&#x8EAB;&#x662F;&#x6CA1;&#x6709;&#x989D;&#x5916;&#x7684;&#x5F00;&#x9500;&#x3002;&#x540C;&#x65F6;&#xFF0C;&#x8BFB;&#x53D6;&#x5FEB;&#x7167;&#x6570;&#x636E;&#x662F;&#x4E0D;&#x9700;&#x8981;&#x4E0A;&#x9501;&#x7684;&#xFF0C;&#x56E0;&#x4E3A;&#x6CA1;&#x6709;&#x4E8B;&#x52A1;&#x9700;&#x8981;&#x5BF9;&#x5386;&#x53F2;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x4FEE;&#x6539;&#x64CD;&#x4F5C;&#x3002;</p>
<p>&#x9694;&#x79BB;&#x7EA7;&#x522B;Read Commited&#x548C;Repeatable read&#x4E0B;&#xFF0C;InnoDB&#x5B58;&#x50A8;&#x5F15;&#x64CE;&#x4F7F;&#x7528;&#x975E;&#x9501;&#x5B9A;&#x7684;&#x4E00;&#x81F4;&#x6027;&#x8BFB;&#xFF0C;&#x4F46;&#x8BFB;&#x53D6;&#x7684;&#x5FEB;&#x7167;&#x6570;&#x636E;&#x4E0D;&#x540C;&#x3002;</p>
<ul>
<li>Read Commited:&#x975E;&#x4E00;&#x81F4;&#x6027;&#x8BFB;&#x603B;&#x662F;&#x8BFB;&#x53D6;&#x88AB;&#x9501;&#x5B9A;&#x884C;&#x7684;&#x6700;&#x65B0;&#x4E00;&#x4EFD;&#x5FEB;&#x7167;&#x6570;&#x636E;</li>
<li>Repeatable read:&#x975E;&#x4E00;&#x81F4;&#x6027;&#x8BFB;&#x603B;&#x662F;&#x8BFB;&#x53D6;&#x4E8B;&#x52A1;&#x5F00;&#x59CB;&#x65F6;&#x7684;&#x884C;&#x6570;&#x636E;&#x7248;&#x672C;</li>
</ul>
<p><strong>repeatable read&#x793A;&#x4F8B;</strong></p>
<table>
<thead>
<tr>
<th>A&#x4F1A;&#x8BDD;</th>
<th style="text-align:left">B&#x4F1A;&#x8BDD;</th>
</tr>
</thead>
<tbody>
<tr>
<td>begin;</td>
<td style="text-align:left"></td>
</tr>
<tr>
<td>select * from account; ## amount = 100</td>
<td style="text-align:left"></td>
</tr>
<tr>
<td></td>
<td style="text-align:left">begin;</td>
</tr>
<tr>
<td></td>
<td style="text-align:left">update account set amount = 200 where amount = 100;</td>
</tr>
<tr>
<td></td>
<td style="text-align:left">commit;</td>
</tr>
<tr>
<td>select * from account;  ## amount = 100</td>
<td style="text-align:left"></td>
</tr>
<tr>
<td>commit;</td>
</tr>
</tbody>
</table>
<p><strong>read commited&#x793A;&#x4F8B;</strong></p>
<table>
<thead>
<tr>
<th>A&#x4F1A;&#x8BDD;</th>
<th style="text-align:left">B&#x4F1A;&#x8BDD;</th>
</tr>
</thead>
<tbody>
<tr>
<td>begin;</td>
<td style="text-align:left"></td>
</tr>
<tr>
<td>select * from account; ## amount = 100</td>
<td style="text-align:left"></td>
</tr>
<tr>
<td></td>
<td style="text-align:left">begin;</td>
</tr>
<tr>
<td></td>
<td style="text-align:left">update account set amount = 200 where amount = 100;</td>
</tr>
<tr>
<td></td>
<td style="text-align:left">commit;</td>
</tr>
<tr>
<td>select * from account;  ## amount = 200</td>
<td style="text-align:left"></td>
</tr>
<tr>
<td>commit;</td>
</tr>
</tbody>
</table>
<hr>
<h2 id="&#x4E00;&#x81F4;&#x6027;&#x9501;&#x5B9A;&#x8BFB;">&#x4E00;&#x81F4;&#x6027;&#x9501;&#x5B9A;&#x8BFB;</h2>
<p>&#x7528;&#x6237;&#x9700;&#x8981;&#x663E;&#x793A;&#x5730;&#x5BF9;&#x6570;&#x636E;&#x5E93;&#x8BFB;&#x53D6;&#x64CD;&#x4F5C;&#x8FDB;&#x884C;&#x52A0;&#x9501;&#x4EE5;&#x4FDD;&#x8BC1;&#x6570;&#x636E;&#x903B;&#x8F91;&#x7684;&#x4E00;&#x81F4;&#x6027;&#x3002;&#x8981;&#x6C42;&#x6570;&#x636E;&#x5E93;&#x652F;&#x6301;&#x52A0;&#x9501;&#x8BED;&#x53E5;&#x3002;</p>
<p>select&#x8BED;&#x53E5;&#x652F;&#x6301;&#x4E24;&#x79CD;&#x4E00;&#x81F4;&#x6027;&#x9501;&#x5B9A;&#x8BFB;&#xFF08;locking reading&#xFF09;&#xFF1A;</p>
<pre><code>select ... for update; 

select ... lock in share mode;
</code></pre><p>insert&#x8BED;&#x53E5;&#x652F;&#x6301;&#xFF1A;</p>
<pre><code>insert ... xxx select xxxx;
</code></pre><p>locking reading&#x4E24;&#x8005;&#x533A;&#x522B;&#xFF1A;</p>
<table>
<thead>
<tr>
<th>&#x8BED;&#x53E5;</th>
<th style="text-align:left">&#x9501;&#x7C7B;&#x578B;</th>
<th style="text-align:left">amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>select ... for update</td>
<td style="text-align:left">&#x6392;&#x4ED6;&#x9501;(X LOCK)</td>
<td style="text-align:left">Mysql&#x4F1A;&#x5BF9;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x4E2D;&#x7684;&#x6BCF;&#x884C;&#x90FD;&#x52A0;&#x6392;&#x4ED6;&#x9501;&#xFF0C;&#x5F53;&#x6CA1;&#x6709;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x5BF9;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x96C6;&#x4E2D;&#x7684;&#x4EFB;&#x4F55;&#x4E00;&#x884C;&#x4F7F;&#x7528;&#x6392;&#x4ED6;&#x9501;&#x65F6;&#xFF0C;&#x53EF;&#x4EE5;&#x6210;&#x529F;&#x7533;&#x8BF7;&#x6392;&#x4ED6;&#x9501;&#xFF0C;&#x5426;&#x5219;&#x4F1A;&#x88AB;&#x963B;&#x585E;&#x3002;&#x884C;&#x7EA7;&#x9501;&#x90FD;&#x662F;&#x57FA;&#x4E8E;&#x7D22;&#x5F15;&#x7684;&#xFF0C;&#x5982;&#x679C;&#x4E00;&#x6761;SQL&#x8BED;&#x53E5;&#x7528;&#x4E0D;&#x5230;&#x7D22;&#x5F15;&#x662F;&#x4E0D;&#x4F1A;&#x4F7F;&#x7528;&#x884C;&#x7EA7;&#x9501;&#x7684;&#xFF0C;&#x4F1A;&#x4F7F;&#x7528;&#x8868;&#x7EA7;&#x9501;&#x3002;&#x884C;&#x7EA7;&#x9501;&#x7684;&#x7F3A;&#x70B9;&#x662F;&#xFF1A;&#x7531;&#x4E8E;&#x9700;&#x8981;&#x8BF7;&#x6C42;&#x5927;&#x91CF;&#x7684;&#x9501;&#x8D44;&#x6E90;&#xFF0C;&#x6240;&#x4EE5;&#x901F;&#x5EA6;&#x6162;&#xFF0C;&#x5185;&#x5B58;&#x6D88;&#x8017;&#x5927;&#xFF01;</td>
</tr>
<tr>
<td>select ... lock in share mode;</td>
<td style="text-align:left">&#x5171;&#x4EAB;&#x9501;(S LOCK)</td>
<td style="text-align:left">Mysql&#x4F1A;&#x5BF9;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x4E2D;&#x7684;&#x6BCF;&#x884C;&#x90FD;&#x52A0;&#x5171;&#x4EAB;&#x9501;&#xFF0C;&#x5F53;&#x6CA1;&#x6709;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x5BF9;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x96C6;&#x4E2D;&#x7684;&#x4EFB;&#x4F55;&#x4E00;&#x884C;&#x4F7F;&#x7528;&#x6392;&#x4ED6;&#x9501;&#x65F6;&#xFF0C;&#x53EF;&#x4EE5;&#x6210;&#x529F;&#x7533;&#x8BF7;&#x5171;&#x4EAB;&#x9501;&#xFF0C;&#x5426;&#x5219;&#x4F1A;&#x88AB;&#x963B;&#x585E;&#x3002;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x4E5F;&#x53EF;&#x4EE5;&#x8BFB;&#x53D6;&#x4F7F;&#x7528;&#x4E86;&#x5171;&#x4EAB;&#x9501;&#x7684;&#x8868;&#xFF0C;&#x800C;&#x4E14;&#x8FD9;&#x4E9B;&#x7EBF;&#x7A0B;&#x8BFB;&#x53D6;&#x7684;&#x662F;&#x540C;&#x4E00;&#x4E2A;&#x7248;&#x672C;&#x7684;&#x6570;&#x636E;&#x3002;</td>
</tr>
</tbody>
</table>
<hr>
<h2 id="&#x5916;&#x952E;&#x4E0E;&#x9501;">&#x5916;&#x952E;&#x4E0E;&#x9501;</h2>
<p>&#x5BF9;&#x4E8E;&#x5916;&#x952E;&#x503C;&#x7684;&#x63D2;&#x5165;&#x4E0E;&#x66F4;&#x65B0;&#xFF0C;&#x9996;&#x5148;&#x9700;&#x8981;&#x67E5;&#x8BE2;&#x7236;&#x8868;&#x7684;select&#x64CD;&#x4F5C;&#xFF0C;&#x8981;&#x6C42;&#x4F7F;&#x7528;&#x4E00;&#x81F4;&#x6027;&#x9501;&#x5B9A;&#x8BFB;&#x3002;</p>
<p>&#x5982;&#x4E0B;&#x4F8B;&#x5B50;&#xFF0C;&#x6709;&#x4E3B;&#x8868;account&#x53CA;&#x5176;&#x5B50;&#x8868;account_detail</p>
<p><strong>account</strong></p>
<table>
<thead>
<tr>
<th>id</th>
<th style="text-align:left">name</th>
<th style="text-align:left">amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td style="text-align:left">tg</td>
<td style="text-align:left">600.00</td>
</tr>
<tr>
<td>2</td>
<td style="text-align:left">mark</td>
<td style="text-align:left">200.00</td>
</tr>
</tbody>
</table>
<p><strong>account_detail</strong></p>
<table>
<thead>
<tr>
<th>id</th>
<th style="text-align:left">account_id</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td style="text-align:left">1</td>
</tr>
</tbody>
</table>
<hr>
<p>&#x4F1A;&#x8BDD;&#x6267;&#x884C;&#xFF1A;</p>
<table>
<thead>
<tr>
<th>A&#x4F1A;&#x8BDD;</th>
<th style="text-align:left">B&#x4F1A;&#x8BDD;</th>
</tr>
</thead>
<tbody>
<tr>
<td>begin;</td>
<td style="text-align:left"></td>
</tr>
<tr>
<td>delete from account where id = 2;</td>
<td style="text-align:left"></td>
</tr>
<tr>
<td></td>
<td style="text-align:left">begin;</td>
</tr>
<tr>
<td></td>
<td style="text-align:left">insert account_detail (account_id) (select id from account where id = 2);</td>
</tr>
<tr>
<td></td>
<td style="text-align:left">&#x5835;&#x585E;</td>
</tr>
</tbody>
</table>
<p>&#x6839;&#x636E;&#x4EE5;&#x4E0A;&#x4F8B;&#x5B50;&#xFF0C;B&#x4F1A;&#x8BDD;&#x6267;&#x884C;insert&#x65F6;&#x4F1A;&#x5835;&#x585E;&#xFF0C;&#x56E0;&#x4E3A;&#x4F1A;&#x8BDD;A&#x83B7;&#x5F97;X&#x9501;&#xFF0C;insert select&#x4E3A;&#x4E00;&#x81F4;&#x6027;&#x8BFB;&#xFF0C;&#x65E0;&#x6CD5;&#x83B7;&#x53D6;&#x4E0E;X&#x4E0D;&#x517C;&#x5BB9;&#x7684;S&#x9501;&#x3002;&#x8BBE;&#x60F3;&#xFF0C;&#x5982;&#x679C;&#x5916;&#x8868;&#x7684;&#x63D2;&#x5165;&#x7ED3;&#x679C;&#xFF0C;&#x662F;&#x901A;&#x8FC7;&#x4E00;&#x81F4;&#x6027;&#x975E;&#x9501;&#x5B9A;&#x8BFB;&#x7684;&#x8BFB;&#x53D6;&#x7ED3;&#x679C;&#xFF0C;&#x4F1A;&#x51FA;&#x73B0;&#x6570;&#x636E;&#x4E0D;&#x4E00;&#x81F4;&#x7684;&#x60C5;&#x51B5;&#x3002;</p>
<table>
<thead>
<tr>
<th>A&#x4F1A;&#x8BDD;</th>
<th style="text-align:left">B&#x4F1A;&#x8BDD;</th>
</tr>
</thead>
<tbody>
<tr>
<td>begin;</td>
<td style="text-align:left"></td>
</tr>
<tr>
<td>delete from account where id = 2;</td>
<td style="text-align:left"></td>
</tr>
<tr>
<td></td>
<td style="text-align:left">begin;</td>
</tr>
<tr>
<td></td>
<td style="text-align:left">select id = @accountId from account where id = 2;</td>
</tr>
<tr>
<td></td>
<td style="text-align:left">insert account_detail (account_id) value (@accountId);</td>
</tr>
<tr>
<td></td>
<td style="text-align:left">commit;</td>
</tr>
<tr>
<td>commit;</td>
</tr>
</tbody>
</table>
<p>&#x7ED3;&#x679C;&#xFF1A;account&#x8868;&#x4E2D;&#xFF0C;id = 2&#x7684;&#x8BB0;&#x5F55;&#x5DF2;&#x88AB;&#x5220;&#x9664;&#xFF0C;&#x4F46;&#x5B50;&#x8868;account_id = 2&#x7684;&#x8BB0;&#x5F55;&#x5374;&#x5E76;&#x88AB;&#x63D2;&#x5165;&#x3002;</p>
<h2 id="&#x7ECF;&#x5178;&#x6B7B;&#x9501;&#x6848;&#x4F8B;">&#x7ECF;&#x5178;&#x6B7B;&#x9501;&#x6848;&#x4F8B;</h2>
<table>
<thead>
<tr>
<th>A&#x4F1A;&#x8BDD;</th>
<th style="text-align:left">B&#x4F1A;&#x8BDD;</th>
</tr>
</thead>
<tbody>
<tr>
<td>begin;</td>
<td style="text-align:left"></td>
</tr>
<tr>
<td>select id from account where user_id = 2 <strong>for update</strong>;</td>
<td style="text-align:left"></td>
</tr>
<tr>
<td></td>
<td style="text-align:left">begin;</td>
</tr>
<tr>
<td></td>
<td style="text-align:left">select id from account where id = 2 for update;</td>
</tr>
<tr>
<td>if(id is null)  #&#x7528;&#x6237;&#x8FD8;&#x6CA1;&#x521B;&#x5EFA;&#x8D26;&#x6237;&#xFF0C;&#x901A;&#x8FC7;</td>
<td style="text-align:left"></td>
</tr>
<tr>
<td>insert into account #&#x7B49;&#x5F85;B&#x4F1A;&#x8BDD;for update&#x91CA;&#x653E;&#x9501;</td>
<td style="text-align:left"></td>
</tr>
<tr>
<td></td>
<td style="text-align:left">if(id is null)  #&#x4F1A;&#x8BDD;A&#x7528;&#x6237;&#x8FD8;&#x6CA1;&#x521B;&#x5EFA;&#x8D26;&#x6237;&#xFF0C;&#x901A;&#x8FC7;</td>
</tr>
<tr>
<td></td>
<td style="text-align:left">insert into account #&#x7B49;&#x5F85;A&#x4F1A;&#x8BDD;insert&#x91CA;&#x653E;&#x9501;&#xFF0C;&#x4E92;&#x76F8;&#x963B;&#x585E;&#xFF0C;&#x89E6;&#x53D1;&#x6B7B;&#x9501;</td>
</tr>
<tr>
<td></td>
<td style="text-align:left">commit;</td>
</tr>
<tr>
<td>commit;</td>
</tr>
</tbody>
</table>
<p><img src="assets/1574914745055.png" alt="1574914745055"></p>
<p>&#x5982;&#x679C;&#x8981;&#x5B9E;&#x73B0;&#x201C;&#x4E0D;&#x5B58;&#x5728;&#x5219;&#x63D2;&#x5165;&#x201D;&#x529F;&#x80FD;&#xFF0C;&#x901A;&#x8FC7;&#x4EE5;&#x4E0B;&#x903B;&#x8F91;&#x662F;&#x4E0D;&#x80FD;&#x5B9E;&#x73B0;&#x7684;</p>
<pre><code>select id for update;#&#x4E0A;&#x9501;X&#x9501;

if(id is null){
    insert into table
}
</code></pre><hr>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; tgor.top 2019 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2020-04-23 15:32:53
</span></footer>
<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="经典数据库设计案例.html" class="navigation navigation-prev " aria-label="Previous page: mysql设计">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="mysql数据类型.html" class="navigation navigation-next " aria-label="Next page: mysql数据类型">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"mysql事务与锁","level":"1.3.2","depth":2,"next":{"title":"mysql数据类型","level":"1.3.3","depth":2,"path":"Mysql/mysql数据类型.md","ref":"Mysql/mysql数据类型.md","articles":[]},"previous":{"title":"mysql设计","level":"1.3.1","depth":2,"path":"Mysql/经典数据库设计案例.md","ref":"Mysql/经典数据库设计案例.md","articles":[]},"dir":"ltr"},"config":{"plugins":["chapter-fold","splitter","tbfed-pagefooter","page-toc-button","-search","search-pro","popup","back-to-top-button","code","copy-code-button"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy tgor.top 2019","modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"chapter-fold":{},"splitter":{},"search-pro":{"cutWordLib":"nodejieba","defineWord":["Gitbook Use"]},"popup":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"code":{"copyButtons":true},"fontsettings":{"theme":"sepia","family":"serif","size":1},"highlight":{},"page-toc-button":{"maxTocDepth":2,"minTocSize":2},"back-to-top-button":{},"copy-code-button":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"tgor","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"tgor note","gitbook":"*","description":"tgor生活工作记录"},"file":{"path":"Mysql/Mysql事务与锁.md","mtime":"2020-04-23T07:32:53.357Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-04-29T08:43:24.127Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-page-toc-button/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-copy-code-button/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

